package com.neueedu.android;

import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.os.Bundle;
//import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity{


    String[]  tabTitles = {"车票","订单","我的"};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setContentView(R.layout.activity_main);

         ViewPager2 viewPager = findViewById(R.id.vp);
        TabLayout tabLayout = findViewById(R.id.tab);


        //生成 选项卡
        for (String tabTitle : tabTitles) {
            tabLayout.addTab(tabLayout.newTab().setText(tabTitle));
        }

        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new TicketFragment());
        fragmentList.add(new OrderFragment());
        fragmentList.add(new MyFragment());

        MyPageApapter adapter = new MyPageApapter(getSupportFragmentManager(),fragmentList);

        viewPager.setAdapter(adapter);


//        tabLayout.setupWithViewPager(viewPager);

        new TabLayoutMediator(tabLayout, viewPager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(tabTitles[position]);
            }
        }).attach();


    }


    //用于提供 ViewPage滑动页面内容
    class MyPageApapter extends FragmentStateAdapter{


        private List<Fragment> fragmentList = null;

        public MyPageApapter(@NonNull FragmentManager fragmentManager,List fragmentList) {
            super(fragmentManager ,  getLifecycle());
            this.fragmentList = fragmentList;
        }

        @NonNull
        @Override
        public Fragment createFragment(int position) {

            return  fragmentList.get(position);
        }

        @Override
        public int getItemCount() {
            return fragmentList.size();
        }
    }




}
